package com.gitee.wsl.common.ui.ext.shape.builder


fun shape(shapeType: ShapeType, builder: AbstractShapeBuilder.()->Unit) {
    val shapeBuilder= AbstractShapeBuilder(shapeType)
    shapeBuilder.builder()
    //return shapeBuilder.b

}

enum class ShapeType {
    RECTANGLE,
    OVAL,
    LINE,
    RING;
}

enum class GradientType {
    LINEAR,
    RADIAL,
    SWEEP;
}

open class AbstractShapeBuilder(val shapeType: ShapeType) {

    private var sizeBuilder = SizeBuilder()
    var strokeBuilder = StrokeBuilder()
    var cornersBuilder = CornersBuilder()
    var paddingBuilder = PaddingBuilder()
    var gradientBuilder = GradientBuilder()


    fun corners(builder: CornersBuilder.() -> Unit): AbstractShapeBuilder {
        cornersBuilder.builder()
        return this
    }

    fun corners(corners: Float): AbstractShapeBuilder {
        cornersBuilder.topLeft = corners
        cornersBuilder.topRight = corners
        cornersBuilder.bottomLeft = corners
        cornersBuilder.bottomRight = corners
        return this
    }

    fun padding(builder: PaddingBuilder.() -> Unit): AbstractShapeBuilder {
        paddingBuilder.builder()
        return this
    }

    fun padding(padding: Int): AbstractShapeBuilder {
        paddingBuilder.left = padding
        paddingBuilder.top = padding
        paddingBuilder.right = padding
        paddingBuilder.bottom = padding
        return this
    }

    fun stroke(builder: StrokeBuilder.() -> Unit): AbstractShapeBuilder {
        strokeBuilder.builder()
        return this
    }

    fun size(width: Int, height: Int): AbstractShapeBuilder {
        sizeBuilder.width = width
        sizeBuilder.height = height
        return this
    }

    fun size(builder: SizeBuilder.() -> Unit): AbstractShapeBuilder {
        sizeBuilder.builder()
        return this
    }

    fun gradient(builder: GradientBuilder.() -> Unit): AbstractShapeBuilder {
        gradientBuilder.builder()
        return this
    }


    class SizeBuilder {
        var width: Int = 0
        var height: Int = 0
    }

    class CornersBuilder {
        var topLeft: Float = 0f
        var topRight: Float = 0f
        var bottomLeft: Float = 0f
        var bottomRight: Float = 0f
    }

    class PaddingBuilder {
        var left: Int = 0
        var top: Int = 0
        var right: Int = 0
        var bottom: Int = 0
    }

    class StrokeBuilder {
        var width: Int = 0
        var color: Int = 0
        private var dash: DashBuilder = DashBuilder()

        var dashWidth: Float by dash::width

        var dashGap: Float by dash::gap

        fun dash(builder: DashBuilder.() -> Unit) {
            dash.builder()
        }

        class DashBuilder {
            var width: Float = 0f
            var gap: Float = 0f
        }
    }

    class GradientBuilder {
        private var color = ColorBuilder()
        var type = GradientType.LINEAR
        var radius: Float = 0f
        var centerX: Float = 0f
        var centerY: Float = 0f
        var useLevel: Boolean = false
        var endColor: Int? by color::end
        var startColor: Int? by color::start
        var centerColor: Int? by color::center

        val colors: IntArray
            get() = listOfNotNull(startColor, centerColor, endColor).toIntArray()

        fun color(color: Int) = color(color, color)

        fun color(start: Int, end: Int, center: Int? = null) {
            color.end = end
            color.start = start
            color.center = center
        }

        fun color(builder: ColorBuilder.() -> Unit) {
            color.builder()
        }

        class ColorBuilder {
         var start: Int? = null
         var center: Int? = null
         var end: Int? = null
        }
    }

}
